import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  ParseUUIDPipe,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { Permissions, PermissionsGuard } from '@nbase/nest-mods';
import type { IDept, IDeptTree, IPaginationResponse } from '@nbase/shared';
import { DeptService } from './dept.service';
import { CreateDeptDto } from './dto/create-dept.dto';
import { UpdateDeptDto } from './dto/update-dept.dto';
import { QueryDeptDto } from './dto/query-dept.dto';

/**
 * 部门控制器
 */
@ApiTags('system/dept')
@ApiBearerAuth('JWT-auth')
@Controller('system/dept')
@UseGuards(PermissionsGuard)
export class DeptController {
  constructor(private readonly deptService: DeptService) {}

  /**
   * 查询所有部门（分页查询）
   */
  @ApiOperation({ summary: '获取部门列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:dept:query')
  @Get()
  findAll(@Query() query: QueryDeptDto): Promise<IPaginationResponse<IDept>> {
    return this.deptService.paginateDepts(query);
  }

  /**
   * 获取部门树
   */
  @ApiOperation({ summary: '获取部门树' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:dept:query')
  @Get('tree')
  getDeptTree(): Promise<IDeptTree[]> {
    return this.deptService.getDeptTree();
  }

  /**
   * 获取部门详情
   */
  @ApiOperation({ summary: '获取部门详情' })
  @ApiParam({ name: 'id', description: '部门ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 404, description: '部门不存在' })
  @Permissions('system:dept:query')
  @Get(':id')
  findOne(@Param('id', ParseUUIDPipe) id: string): Promise<IDept> {
    return this.deptService.findById(id);
  }

  /**
   * 根据编码获取部门
   */
  @ApiOperation({ summary: '根据编码获取部门' })
  @ApiParam({ name: 'code', description: '部门编码', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:dept:query')
  @Get('code/:code')
  findByCode(@Param('code') code: string): Promise<IDept | null> {
    return this.deptService.findByCode(code);
  }

  /**
   * 创建部门
   */
  @ApiOperation({ summary: '创建部门' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiResponse({ status: 400, description: '参数错误' })
  @Permissions('system:dept:create')
  @Post()
  create(@Body() createDeptDto: CreateDeptDto): Promise<IDept> {
    return this.deptService.createDept(createDeptDto);
  }

  /**
   * 更新部门
   */
  @ApiOperation({ summary: '更新部门' })
  @ApiParam({ name: 'id', description: '部门ID', type: String })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiResponse({ status: 404, description: '部门不存在' })
  @Permissions('system:dept:update')
  @Put(':id')
  update(@Param('id', ParseUUIDPipe) id: string, @Body() updateDeptDto: UpdateDeptDto): Promise<IDept> {
    return this.deptService.updateDept(id, updateDeptDto);
  }

  /**
   * 删除部门
   */
  @ApiOperation({ summary: '删除部门' })
  @ApiParam({ name: 'id', description: '部门ID', type: String })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '部门不存在' })
  @Permissions('system:dept:delete')
  @Delete(':id')
  async remove(@Param('id', ParseUUIDPipe) id: string): Promise<void> {
    await this.deptService.deleteDept(id);
  }
}

